from rest_framework import serializers
from .models import *
from django.utils import timezone
from django.utils.timezone import localtime
from datetime import datetime


class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = UserModel
        fields = ['nickname', 'openid', 'avatar', 'introduct', 'grade', 'collage', 'fans', 'attentions']


class ShopSerializer(serializers.ModelSerializer):
    author = UserSerializer()

    class Meta:
        model = ShopModel
        fields = ['name', 'price', 'picture', 'author']


class OrderSerializer(serializers.ModelSerializer):
    class Meta:
        model = OrderModel
        fields = ['id', 'shop', 'author', 'price', 'time', 'status']
        depth = 1


# 帖子列表
class PostSerializer(serializers.ModelSerializer):
    author = serializers.SerializerMethodField()
    pictures = serializers.SerializerMethodField()
    pub_time_local = serializers.SerializerMethodField()  # 新增一个用于显示本地化时间的字段

    class Meta:
        model = PostModel
        fields = ['id', 'author', 'content', 'pub_time', 'likes', 'pictures', 'pub_time_local']  # 添加pub_time_local字段

    def get_author(self, obj):
        return {
            'nickname': obj.author.nickname,
            'avatar': obj.author.avatar.url,
            'openid': obj.author.openid,
            'introduct': obj.author.introduct,
            'collage': obj.author.collage,
            'grade': obj.author.grade,
            'fans': obj.author.fans,
            'attentions': obj.author.attentions,
        }

    def get_pictures(self, obj):
        pictures = PostPictureModel.objects.filter(post=obj)
        return [picture.picture.url for picture in pictures]

    def get_pub_time_local(self, obj):  # 修改序列化方法以去除时区信息并格式化时间
        local_time = localtime(obj.pub_time)
        formatted_time = datetime.strftime(local_time, '%Y-%m-%d %H:%M')
        return formatted_time


# 帖子
# class PostSerializer(serializers.ModelSerializer):
#     author = UserSerializer(read_only=True)  # 序列化外键字段为用户信息
#
#     class Meta:
#         model = PostModel
#         fields = ['id', 'author', 'content', 'pub_time', 'likes']

# 评论
class CommentSerializer(serializers.ModelSerializer):
    author = serializers.SerializerMethodField()
    pub_time_local = serializers.SerializerMethodField()  # 新增一个用于显示本地化时间的字段

    class Meta:
        model = CommentModel
        fields = ['id', 'post', 'author', 'content', 'pub_time', 'pub_time_local']

    def get_author(self, obj):
        return {
            'nickname': obj.author.nickname,
            'avatar': obj.author.avatar.url,
            # 若有 openid 字段则添加，如果没有请忽略此行
            'openid': obj.author.openid
        }

    def get_pub_time_local(self, obj):
        utc_time = obj.pub_time  # 假设 pub_time 字段已经是 UTC 格式的时间戳
        if timezone.is_aware(utc_time):
            local_time = utc_time.astimezone(timezone.get_default_timezone())  # 使用默认时区
        else:
            # 如果 pub_time 是本地时间，那么就不需要转换，直接格式化即可
            local_time = utc_time

        formatted_time = local_time.strftime('%m-%d %H:%M')
        return formatted_time


# 首页轮播图

class BannerSerializer(serializers.ModelSerializer):
    class Meta:
        model = BannerModel
        fields = "__all__"


# 二手商品
class ShopModelSerializer(serializers.ModelSerializer):
    class Meta:
        model = ShopModel
        fields = '__all__'
        depth = 1


# 点赞事件
class LikeSerializer(serializers.ModelSerializer):
    author_nickname = serializers.CharField(source='author.nickname')
    author_avatar = serializers.ImageField(source='author.avatar')

    class Meta:
        model = LikeModel
        fields = ['author_nickname', 'author_avatar', 'time']


# 商家信息
class ShopInfoSerializer(serializers.ModelSerializer):
    class Meta:
        model = ShopInfoModel
        fields = '__all__'


# 关注
class FollowSerializer(serializers.ModelSerializer):
    class Meta:
        model = FollowModel
        fields = '__all__'
        depth = 1


# 圈子
class CircleSerializer(serializers.ModelSerializer):
    class Meta:
        model = CircleModel
        fields = '__all__'
        depth = 1


# 圈子成员
class CircleMemberSerializer(serializers.ModelSerializer):
    class Meta:
        model = CircleMemberModel
        fields = '__all__'
        depth = 1


# 圈子帖子
class CirclePostSerializer(serializers.ModelSerializer):
    class Meta:
        model = CirclePostModel
        fields = '__all__'
        depth = 1


# 圈子申请帖子
class CircleApplySerializer(serializers.ModelSerializer):
    class Meta:
        model = CircleApplyModel
        fields = '__all__'
        depth = 1
